﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecopsy.Tools;

public partial class Ctl_ctlPeriodTypeSelector : System.Web.UI.UserControl
{
    // максимальный период демонстрации в режиме дни 
    private int MaxDayDiff_Day = 14;

    // максимальный период демонстрации в режиме недели   
    private int MaxDayDiff_Week = 16;

    // максимальный период демонстрации в режиме недели   
    private int MaxDayDiff_Month= 12;

    #region Property
    public Ecopsy.Tools.DateTimeTools.TimePeriod DetailesPeriod
    {
        get {
            switch (hfCurrentPeriodType.Value)
            {
                case "0":
                    return DateTimeTools.TimePeriod.Day;
                case "1":
                    return DateTimeTools.TimePeriod.Week;
                case "2":
                    return DateTimeTools.TimePeriod.Month;
            }
            return DateTimeTools.TimePeriod.Day;

         
        }
        set 
        {
            switch (value)
            { 
                case DateTimeTools.TimePeriod .Day:
                    hfCurrentPeriodType.Value ="0" ;
                    break ;
                case DateTimeTools.TimePeriod .Week   :
                    hfCurrentPeriodType.Value ="1" ;
                    break ;
                case DateTimeTools.TimePeriod .Month  :
                    hfCurrentPeriodType.Value ="2" ;
                    break ;
            }
        }
    }


    public DateTime AnchorDay 
    {
        get 
        {
            
            if(string.IsNullOrEmpty ( hfAnchorDay .Value ))
                return DateTime .Now ;

            return Convert.ToDateTime(hfAnchorDay.Value);
        }
        set 
        {
            hfAnchorDay.Value = value.ToShortDateString();
        }
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        if (DetailesPeriod == DateTimeTools.TimePeriod.Day)
            MultiView1.ActiveViewIndex = 0;
        else
            MultiView1.ActiveViewIndex = 1; 

        switch (DetailesPeriod )
        {
            case DateTimeTools.TimePeriod .Day :
                    DataPopulate_Day();
                    break;
            case DateTimeTools.TimePeriod.Week :
                    DataPopulate_Week();
                    break;
            case DateTimeTools.TimePeriod.Month :
                    DataPopulate_Month();
                    break;
        }

    }


    protected string btnPrevNext_Hint (bool Forvard)
    { 
    
        switch (DetailesPeriod)
        { 
            case DateTimeTools.TimePeriod.Day :
                return "Месяц "+(Forvard?"вперед":"назад");   
                
            case DateTimeTools.TimePeriod.Week :
                return "16 недель " + (Forvard ? "вперед" : "назад");   
                
            case DateTimeTools.TimePeriod.Month:
                return "Год " + (Forvard ? "вперед" : "назад"); 
        
    }
        return string.Empty;

    }

    private void DataPopulate_Day ()
    {

        txtDtStart.Text = AnchorDay.ToShortDateString();
       //txtDtStart.Text = DtStart.ToShortDateString ();
       ////txtDtEnd.Text = DtEnd .ToShortDateString (); 
       // cmbFirst.Items.Clear();
       // cmbSecond.Items.Clear();

        //DateTime dt = Ecopsy.Tools.DateTimeTools.GetFisrtDayInMonth(AnchorDay);

        //for (int i = 0; i < MaxDayDiff_Month; i++)
        //{
        //    DateTime current = dt.AddMonths(i);
        //    if (current.Day == 1)
        //    {
        //        ListItem f = new ListItem { Text = current.ToString("Y"), Value = current.ToShortDateString() };
        //        cmbFirst.Items.Add(f);

        //        ListItem s = new ListItem { Text = current.AddMonths(1).ToString("Y"), Value = current.AddMonths(1).AddDays(-1).ToShortDateString() };
        //        cmbSecond.Items.Add(s);
        //    }
        //}

    }

    private void DataPopulate_Week()
    {
        cmbFirst.Items.Clear();
        cmbSecond.Items.Clear();

        int count = MaxDayDiff_Week * 7;
        DateTime dt= Ecopsy.Tools.DateTimeTools.GetFisrtDayInWeek(AnchorDay);         

        for (int i = 0; i < count; i++)
        {
            DateTime current = dt.AddDays(i);
            if (current.DayOfWeek == DayOfWeek.Monday)
            {
                ListItem s = new ListItem { Text = "Пн. " + current.ToString("M"), Value = current.ToShortDateString() };
                cmbFirst.Items.Add(s);
            }

            if (current.DayOfWeek == DayOfWeek.Sunday)
            {
                ListItem f = new ListItem { Text = "Вс. " + current.ToString("M"), Value = current.ToShortDateString() };
                cmbSecond.Items.Add(f);
            }
        }


    }

    private void DataPopulate_Month()
    {
        cmbFirst.Items.Clear();
        cmbSecond.Items.Clear();
                
        DateTime dt = Ecopsy.Tools.DateTimeTools.GetFisrtDayInMonth(AnchorDay);

        for (int i = 0; i < MaxDayDiff_Month; i++)
        {
            DateTime current = dt.AddMonths(i);
            if (current.Day == 1)
            {
                ListItem f=new ListItem {Text =current .ToString("Y"),Value =current .ToShortDateString ()};                     
                cmbFirst.Items.Add(f);

                ListItem s=new ListItem {Text =current.AddMonths(1).ToString("Y"),Value =current.AddMonths(1).AddDays(-1).ToShortDateString ()}; 
                cmbSecond.Items.Add(s);
            }
        }

    }

    protected string PeriodAsString
    {
        get
        {
            switch (DetailesPeriod)
            {
                case DateTimeTools.TimePeriod.Day:
                    return "день";

                case DateTimeTools.TimePeriod.Week:
                    return "неделя";

                case DateTimeTools.TimePeriod.Month:
                    return "месяц";
            }
            return string.Empty;
        }
    }

    public DateTime DtStart
    {
        get
        {
            if (DetailesPeriod == DateTimeTools.TimePeriod.Day)
            {
                if (String.IsNullOrEmpty(txtDtStart.Text))
                    return AnchorDay;

                return Convert.ToDateTime(txtDtStart.Text);
            }
            else
                return Convert.ToDateTime(cmbFirst.SelectedItem.Value);            
        }
        set
        {
            if (DetailesPeriod == DateTimeTools.TimePeriod.Day)            
                txtDtStart .Text = value.ToShortDateString();  


        }
    }

    public DateTime DtEnd
    {
        get
        {
            if (DetailesPeriod == DateTimeTools.TimePeriod.Day)
            {
                if (String.IsNullOrEmpty(txtDtEnd.Text))
                    return AnchorDay.AddDays (MaxDayDiff_Day);

                return Convert.ToDateTime(txtDtEnd.Text);
            }
            else
                return Convert.ToDateTime(cmbSecond.SelectedItem.Value);            
        }

        set
        {
            if (DetailesPeriod == DateTimeTools.TimePeriod.Day)
                txtDtEnd.Text = value.ToShortDateString();

        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    //protected void OnChangeMode(object sender, EventArgs e)
    //{
    //    if (MultiView1.ActiveViewIndex == 0) MultiView1.ActiveViewIndex = 1; else MultiView1.ActiveViewIndex = 0;

    //}


    protected void IncrementPeriod_Click(object sender, ImageClickEventArgs e)
    {
        
    }
    protected void DecrementPeriod_Click(object sender, ImageClickEventArgs e)
    {

    }


    protected void btnChangeDetailes_Click(object sender, EventArgs e)
    {
        switch (DetailesPeriod)
        { 
            case DateTimeTools.TimePeriod .Day :
                DetailesPeriod = DateTimeTools.TimePeriod.Week;                
                break;

            case DateTimeTools.TimePeriod.Week :
                DetailesPeriod = DateTimeTools.TimePeriod.Month ;
                break;

            case DateTimeTools.TimePeriod.Month :                
                
                txtDtStart.Text = DateTimeTools.GetFisrtDayInMonth(DtStart).ToShortDateString ();
                txtDtEnd.Text  = DateTimeTools.GetLastDayInMonth(DtStart).ToShortDateString (); 

                DetailesPeriod = DateTimeTools.TimePeriod.Day ;
                break;
        }
        DataBind();
    }

    

    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ChangeDateSet(-1);
        
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ChangeDateSet(1);
    }

    private void ChangeDateSet(int Sign)
    {
        int i = 0;
        switch (DetailesPeriod)
        {
            case DateTimeTools.TimePeriod.Day:                
                AnchorDay = DtStart.AddMonths(Sign);
                DtStart = AnchorDay;
                DtEnd = DtEnd.AddMonths(Sign);
                break;

            case DateTimeTools.TimePeriod.Week:
                AnchorDay = DtStart.AddDays ( MaxDayDiff_Week*7*Sign);
                DtStart = AnchorDay;
                DtEnd = DtStart.AddDays(MaxDayDiff_Week * 7 * Sign); 
                break;

            case DateTimeTools.TimePeriod.Month:            
                i = MaxDayDiff_Month * Sign;
                AnchorDay = AnchorDay.AddMonths(i);
                break;
        }
        DataBind();
        
    }
}
